//
//  HotSaleCollectionReusableView.m
//  UGoods
//
//  Created by dllo on 16/1/17.
//  Copyright © 2016年 蓝鸥科技.李贺. All rights reserved.
//

#import "HotSaleCollectionReusableView.h"
#import "LHAFNetworkTool.h"
#import "FindModel.h"
#import "BaseCollectionView.h"
#import "GoodsCollectionViewCell2.h"
#import "BaseLabel.h"
#import "BuyDetailViewController.h"
#import "ConstantsDefine.h"
@interface HotSaleCollectionReusableView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) BaseCollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataArray;

@end

@implementation HotSaleCollectionReusableView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self handledata];
        [self createCollectionView];
        BaseLabel *label1 = [[BaseLabel alloc] initWithFrame:CGRectMake(10 * WIDTH, 0, self.frame.size.width, 30 * HEIGHT)];
        label1.text = @"全场热卖";
        label1.textAlignment = NSTextAlignmentLeft;
        label1.font = [UIFont systemFontOfSize:16 * WIDTH];
        [self addSubview:label1];
        
        BaseLabel *label2 = [[BaseLabel alloc] initWithFrame:CGRectMake(10 * WIDTH, 200 * HEIGHT, self.frame.size.width, 30 * HEIGHT)];
        label2.text = @"新品上架";
        label2.textAlignment = NSTextAlignmentLeft;
        label2.font = [UIFont systemFontOfSize:16 * WIDTH];
        [self addSubview:label2];
        
    }
    return self;
}

- (void)handledata {
    NSString *str = @"http://api.ujipin.com/v2/app/ios/phone/v3/list/getHostListData?channel=appstore&idfa=1EF25F55-6367-4567-B0D5-82DF4F1557CD&user_id=470825&plantform=iphone&version=314&page_size=20&page=1";
    [LHAFNetworkTool getUrl:str body:nil response:LHJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        NSArray *array = resposeObject[@"data"][@"goods_list"];
        self.dataArray = [FindModel modelHandleWithArray:array];
        [self.collectionView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"请求失败：%@", error);
    }];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    layout.itemSize = CGSizeMake(130 * WIDTH, 170 * HEIGHT);
    layout.minimumInteritemSpacing = 10 * WIDTH;
    layout.minimumLineSpacing = 10 * HEIGHT;
    layout.sectionInset = UIEdgeInsetsMake(0, 5 * WIDTH, 0, 5 * WIDTH);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 30 * HEIGHT, self.frame.size.width, 170 * HEIGHT) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    //        self.collectionView.bounces = NO;
    [self addSubview:_collectionView];
    [_collectionView release];
    [_collectionView registerClass:[GoodsCollectionViewCell2 class] forCellWithReuseIdentifier:@"cell"];
}

// cell 个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}

// cell设置
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // section: 分区
    // item：表示cell
    
    // 获取cell
    GoodsCollectionViewCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    FindModel *model = _dataArray[indexPath.item];
    cell.model = model;
//    cell.backgroundColor = [UIColor redColor];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    FindModel *model = _dataArray[indexPath.item];
    /* 协议传值 */
    if ([self.delegate respondsToSelector:@selector(pushBuyDetailWithModel:)]) {
        [self.delegate pushBuyDetailWithModel:model];
    }
    
}



@end
